package com.learn2.other;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 15:25 2018-11-15
 * @Modify by:
 */
public class DemoInterceptor implements HandlerInterceptor {



  @Override
  public boolean preHandle(HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse, Object o) throws Exception {
    long startTimes = System.currentTimeMillis();
    httpServletRequest.setAttribute("startTimes",startTimes);
    return true;
  }

  @Override
  public void postHandle(HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView)
      throws Exception {
    long endTimes = System.currentTimeMillis();
    long startTimes = (long)httpServletRequest.getAttribute("startTimes");
    long  handleTime=endTimes-startTimes;
    System.out.println("处理时间为"+handleTime);
    httpServletRequest.removeAttribute("startTimes");
    httpServletRequest.setAttribute("handleTime",handleTime);


  }

  @Override
  public void afterCompletion(HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

  }
}
